﻿using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Validation.sp2010.Classes;

namespace Validation.sp2010.ControlTemplates.Validation.sp2010
{
    public partial class GetUser : UserControl
    {
        #region public properties
        public bool MultiSelect { get; set; }
        public bool EnableGroup { get; set; }
        public bool WithSecondaryValue { get; set; }

        public List<SPField> Fields { get; set; }
        public List<CoreProperty> UserProfileProeprties { get; set; }

        public StepPrincipal GetPrincipal()
        {
            if (TypeUserValidation.SelectedIndex == 0 && PeopleUser.Accounts.Count == 0)
            {
                ErrorActor.Visible = true;
                return null;
            }
            else
                ErrorActor.Visible = false;

            if (WithSecondaryValue)
            {
                if (TypeUserValidation.SelectedIndex == 1 && PeopleColumnUserEmpty.Accounts.Count == 0)
                {
                    ErrorActorColumn.Visible = true;
                    return null;
                }
                else
                    ErrorActorColumn.Visible = false;


                if (TypeUserValidation.SelectedIndex == 2 && PeopleUserProfileEmpty.Accounts.Count == 0)
                {
                    ErrorActorProfile.Visible = true;
                    return null;
                }
                else
                    ErrorActorProfile.Visible = false; 
            }

            StepPrincipal principal = new StepPrincipal();
            principal.ColumnUser = (TypeUserValidation.SelectedIndex == 1) ? Columns.SelectedValue : ColumnsUserProfile.SelectedValue;
            principal.DisplayColumnUser = (TypeUserValidation.SelectedIndex == 1) ? Columns.SelectedItem.Text : ColumnsUserProfile.SelectedItem.Text;
            principal.UserProfilePropertyName = UserProfilëProperty.SelectedValue;
            principal.DisplayUserProfilePropertyName = UserProfilëProperty.SelectedItem.Text;
            principal.TypeUser = (TypeUser)(int.Parse(TypeUserValidation.SelectedValue));

            if (WithSecondaryValue)
            {
                if (TypeUserValidation.SelectedIndex == 1)
                {
                    principal.SecondaryValidator = PeopleColumnUserEmpty.Accounts[0].ToString();
                    principal.DisplaySecondaryValidator = ((PickerEntity)PeopleColumnUserEmpty.Entities[0]).Description;
                }
                if (TypeUserValidation.SelectedIndex == 2)
                {
                    principal.SecondaryValidator = PeopleUserProfileEmpty.Accounts[0].ToString();
                    principal.DisplaySecondaryValidator = ((PickerEntity)PeopleUserProfileEmpty.Entities[0]).Description;

                }
            }

            principal.StaticUser=new List<string>();

            foreach(var account in PeopleUser.Accounts)
            {
                principal.StaticUser.Add(account.ToString());
            }

            return principal;
        }
        
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (ViewState["loaded"]==null)
            {
                PeopleUser.MultiSelect = MultiSelect;
                if (!EnableGroup)
                    PeopleUser.SelectionSet = "User";

                //mask if mail
                if (!WithSecondaryValue)
                {
                    LiteralColumn.Visible = false;
                    LiteralUserProfile.Visible = false;
                    PeopleColumnUserEmpty.Visible = false;
                    PeopleUserProfileEmpty.Visible = false;
                }

                Columns.DataSource = Fields;
                Columns.DataBind();

                ColumnsUserProfile.DataSource = Fields;
                ColumnsUserProfile.DataBind();

                UserProfilëProperty.DataSource = UserProfileProeprties;
                UserProfilëProperty.DataBind();
                ViewState["loaded"] = true;
            }
        }

        
    }
}
